#include "trap.h"
.set noat
.set noreorder
.globl main
.text

main:
    # 初始化寄存器
    ori $at, $zero, 0x100      # $at ($1) = 0x100
    sw  $at, 0x0($zero)        # 将 $at 的值存储到内存地址 0x0
    ori $at, $zero, 0x200      # $at ($1) = 0x200
    syscall                    # 触发系统调用异常，跳转到异常处理程序入口地址 0x100

    # 异常返回后继续执行
    lw  $at, 0x0($zero)        # $at ($1) = 0x100
    lui $v1, 0xffff            # $v1 ($2) = 0xffff0000
    lui $v0, 0x8000            # $v0 ($3) = 0x80000000
    add $a0, $v1, $v0          # $a0 ($4) = $v1 + $v0，发生溢出
    j success
    nop

# 异常处理程序
.org 0x380
    mfc0 $a1, $13               # $a1 ($5) = cause, cause[6:2] = 0x08
    ori  $a2, $zero, 0x20       # $a2 ($6) = 0x00000020
    beq  $a1, $a2, exc_eret     # 如果 cause 寄存器的值为 0x20，跳转到 exc_eret
    nop
    mfc0 $a1, $13               # $a1 ($5) = cause, cause[6:2] = 0x0C
    ori  $a2, $zero, 0x30       # $a2 ($6) = 0x00000030
    beq  $a1, $a2, exc_eret     # 如果 cause 寄存器的值为 0x30，跳转到 exc_eret
    nop
    j    error                  # 否则跳转到 error
    nop

# 错误处理
error:
    ori $a3, $zero, 0xffff      # $a3 ($7) = 0x0000ffff
    j exc_eret
    nop

# 成功处理
success:
    add $a0, $v1, $at           # $a0 ($4) = 0xffff0100
    HIT_GOOD_TRAP
    nop

# 异常返回处理
exc_eret:
    addiu $a3, $a3, 0x1
    mfc0  $t1, $14
    ori   $t0, $zero, 0x4
    add   $t1, $t1, $t0
    mtc0  $t1, $14
    nop
.set mips32
    eret
.set mips1
    nop
    nop
    nop

